<template>
  <view class="container">
    <u-navbar
      :is-fixed="true"
      :border-bottom="false"
      :is-back="false"
      :background="{ background: '#5697F1' }"
      title="个人中心"
      title-color="#fff"
    >
    </u-navbar>
		<u-toast ref="uToast" />
    <view class="header" v-if="userInfo.id">
      <image class="avator" src="/static/images/user.svg"></image>
      <view>{{userInfo.realname}}</view>
    </view>
    <view class="header unlogin" v-if="!userInfo.id">
      <view class="avator">无</view>
      <view @click="toLogin">请先登录</view>
    </view>
    <view class="btns" v-if="userInfo.id">
      <view class="flex-between" @click="updatePhone">
        <view>修改手机号</view>
        <image class="img" src="/static/images/arrow.svg"></image>
      </view>
      <view class="flex-between" @click="updatePassword">
        <view>修改密码</view>
        <image class="img" src="/static/images/arrow.svg"></image>
      </view>
      <view class="flex-between" @click="addPass">
        <view>添加乘车人</view>
        <image class="img" src="/static/images/arrow.svg"></image>
      </view>
      <view class="flex-between" @click="rz">
        <view>认证学生/残军</view>
        <image class="img" src="/static/images/arrow.svg"></image>
      </view>
      <button class="submit" @click="logout">退出登录</button>
    </view>
  </view>
</template>

<script>
import { getData } from '../../api/system/api' 
export default {
  data() {
    return {
      userInfo: {}
    };
  },
  onLoad(options) {
    console.log(options)
  },
  mounted() {
    uni.getStorage({     
      key:'userInfo',
      success: (res) => {
        this.userInfo = res.data;
      }
    })
    setInterval(() => {
      uni.getStorage({     
        key:'userInfo',
        success: (res) => {
          this.userInfo = res.data;
        }
      })
    }, 3000)
  },
  methods: {
    logout() {
      // 删除storage
      uni.removeStorage({    //删除Storage
        key:'userInfo',
        success: () => {
			    this.$u.route('/pages/interest/login')
        }
      })
    },
    toLogin() {
			this.$u.route('/pages/interest/login')
    },
    rz() {
      getData({
        id: this.userInfo.id
      }).then(res => {
        this.userInfo = res.data.data[0]
        if (this.userInfo.pType === '0' || this.userInfo.pType === '1') {
          this.$refs.uToast.show({
            title: '成人或儿童无须认证',
            type: 'success',
          })
          return
        }
        if (this.userInfo.pType !== '0' && this.userInfo.examine === 'true') {
          this.$refs.uToast.show({
            title: '您已完成认证',
            type: 'success',
          })
          return
        }
        // 前往认证页面
        this.$u.route('/pages/interest/renzheng')
      })
    },
    addPass() {
      this.$u.route('/pages/interest/passList')
    },
    updatePassword() {
      this.$u.route('/pages/interest/updatePassword', {
        userInfo: JSON.stringify(this.userInfo)
      })
    },
    updatePhone() {
      this.$u.route('/pages/interest/updatePhone', {
        userInfo: JSON.stringify(this.userInfo)
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  overflow: hidden;
  
  .header {
    display: flex;
    align-items: center;
    padding: 30rpx;
    background: #f5eaea;

    .avator {
      width: 80rpx;
      height: 80rpx;
      line-height: 80rpx;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      margin-right: 30rpx;
    }

    .unlogin .avator {
      background: #7d8e8e;
    }
  }
}

.btns {
  padding: 30rpx;
}
.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1rpx solid lightblue;
  padding-bottom: 20rpx;

  .img {
    width: 60rpx;
    height: 60rpx;
  }
}

.submit {
  margin: 60rpx 30rpx 0;
  border: none;
  width: calc(100% - 60rpx);
  height: 86rpx;
  line-height: 86rpx;
  box-sizing: border-box;
  border-radius: 15rpx;
  background-color: #5f88ff;
  color: #ffffff;

  &::after {
    content: none;
  }

  &::before {
    content: none;
  }

  &[disabled='true'] {
    background: #e4e4e4;
    font-size: 36rpx;
    font-family: Source Han Sans CN;
    font-weight: 500;
    color: #ffffff;
  }
}
</style>
